Advertisement
tjtr33

fb2eckert03

Mar 18th, 2020
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.29 KB | None | 0 0
  1. /*
  2. * tjp from https://gist.github.com/FredEckert/3425429
  3. * 14mar202 runs on rpi well
  4. * i want arcs and widgets, so lookig at 'nuklear; to get a lot done for me
  5. *
  6. * ver 1 had pixel and line dwg ok
  7. * but had to limit y range to vinfo.yres-7 huh?
  8. * this is ver 2 add point line rect filledrect circle filledcrcle DONE
  9. * TODO: add arc, line width, mouse, btn,led, textbox....
  10. *
  11. To test that the Linux framebuffer is set up correctly, and that the device permissions
  12. are correct, use the program below which opens the frame buffer and draws a gradient-
  13. filled red square:
  14. retrieved from:
  15. Testing the Linux Framebuffer for Qtopia Core (qt4-x11-4.2.2)
  16. http://cep.xor.aps.anl.gov/software/qt4-x11-4.2.2/qtopiacore-testingframebuffer.html
  17.  
  18. * tjp this builds ok and works on ^altF1
  19. * isee a square maybe 200x200 with reddish gradient ll corner to tr corner
  20. * why so small? becuz the code asid be small
  21. * now full screen except for the command line at bottom (1366*768)
  22. * */
  23.  
  24. #include <stdlib.h>
  25. #include <unistd.h>
  26. #include <stdio.h>
  27. #include <fcntl.h>
  28. #include <linux/fb.h>
  29. #include <sys/mman.h>
  30. #include <sys/ioctl.h>
  31.  
  32. // 'global' variables to store screen info
  33.  
  34. struct fb_var_screeninfo vinfo;
  35. struct fb_fix_screeninfo finfo;
  36. char *fbp = 0;
  37.  
  38.  
  39. // helper function to 'plot' a pixel in given color
  40. // tjp clean up the cr cg cu ca, make it one thing and pass ptr to it
  41. // tjp this is now hard coded for 32bpp, and ca doesnt wokr yet ( transparency )
  42. void put_pixel(int x, int y, int cr, int cg, int cu, int ca)
  43. {
  44. long int location = 0;
  45. int myx, myy, yfudge;
  46. yfudge = 7;
  47. myx = x+vinfo.xoffset;
  48. myy = y+vinfo.yoffset;
  49.  
  50. // CLIP all dwg to FRAME BUFFER BOUNDS INCLUDING DANGER SONE AT BOTTOM 'FUDGE'
  51. if ((myx < vinfo.xres) && ( myx > -1) && (myy < vinfo.yres - yfudge) && (myy > -1 )) {
  52.  
  53. location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +
  54. (y+vinfo.yoffset) * finfo.line_length;
  55. *(fbp + location) = cr;
  56. *(fbp + location + 1) = cg;
  57. *(fbp + location + 2) = cu;
  58. *(fbp + location + 3) = ca;
  59. }
  60. }
  61.  
  62. // (uses Bresenham's line algorithm)
  63. void draw_line(int x0, int y0, int x1, int y1, int cr, int cg, int cu,int ca )
  64. {
  65. int dx;
  66. int dy;
  67. int sx;
  68. int sy;
  69. int err;
  70. int e2;
  71. int done;
  72.  
  73. dx = x1 - x0;
  74. dx = (dx >= 0) ? dx : -dx; // abs()
  75. dy = y1 - y0;
  76. dy = (dy >= 0) ? dy : -dy; // abs()
  77.  
  78. if (x0 < x1)
  79. sx = 1;
  80. else
  81. sx = -1;
  82. if (y0 < y1)
  83. sy = 1;
  84. else
  85. sy = -1;
  86.  
  87. err = dx - dy;
  88. done = 0;
  89.  
  90. while (!done) {
  91. put_pixel(x0, y0, cr, cg, cu, ca );
  92. if ((x0 == x1) && (y0 == y1))
  93. done = 1;
  94. else {
  95. e2 = 2 * err;
  96. if (e2 > -dy) {
  97. err = err - dy;
  98. x0 = x0 + sx;
  99. }
  100. if (e2 < dx) {
  101. err = err + dx;
  102. y0 = y0 + sy;
  103. }
  104. }
  105. }
  106. }
  107.  
  108. // helper function to draw a rectangle outline in given color
  109. void draw_rect(int x0, int y0, int w, int h, int cr, int cg, int cu, int ca) {
  110. draw_line(x0, y0, x0 + w, y0, cr, cg, cu, ca); // top
  111. draw_line(x0, y0, x0, y0 + h, cr, cg, cu, ca); // left
  112. draw_line(x0, y0 + h, x0 + w, y0 + h, cr, cg, cu, ca); // bottom
  113. draw_line(x0 + w, y0, x0 + w, y0 + h, cr, cg, cu, ca); // right
  114. }
  115.  
  116. // helper function to draw a rectangle outline in given color
  117. void fill_rect(int x0, int y0, int w, int h, int cr, int cg, int cu, int ca) {
  118. int y;
  119. for (y = 0; y < h; y++) {
  120. draw_line(x0, y0 + y, x0 + w, y0 + y, cr, cg, cu, ca);
  121. }
  122. }
  123.  
  124. // helper function to draw a circle outline in given color
  125. // (uses Bresenham's circle algorithm)
  126. void draw_circle(int x0, int y0, int r, int cr, int cg, int cu, int ca)
  127. {
  128. int x = r;
  129. int y = 0;
  130. int radiusError = 1 - x;
  131.  
  132. while(x >= y)
  133. {
  134. put_pixel(-y + x0, -x + y0, ca, cg, cu, ca);// top left
  135. put_pixel( y + x0, -x + y0, ca, cg, cu, ca);// top right
  136. put_pixel(-x + x0, -y + y0, ca, cg, cu, ca);// upper middle left
  137. put_pixel( x + x0, -y + y0, ca, cg, cu, ca);// upper middle right
  138. put_pixel(-x + x0, y + y0, ca, cg, cu, ca);// lower middle left
  139. put_pixel( x + x0, y + y0, ca, cg, cu, ca);// lower middle right
  140. put_pixel(-y + x0, x + y0, ca, cg, cu, ca);// bottom left
  141. put_pixel( y + x0, x + y0, ca, cg, cu, ca);// bottom right
  142.  
  143. y++;
  144. if (radiusError < 0)
  145. {
  146. radiusError += 2 * y + 1;
  147. } else {
  148. x--;
  149. radiusError+= 2 * (y - x + 1);
  150. }
  151. }
  152. }
  153.  
  154. // helper function to draw a filled circle in given color
  155. // (uses Bresenham's circle algorithm)
  156. void fill_circle(int x0, int y0, int r, int cr, int cg, int cu, int ca) {
  157. int x = r;
  158. int y = 0;
  159. int radiusError = 1 - x;
  160.  
  161. while(x >= y)
  162. {
  163. draw_line(-y + x0, -x + y0, y + x0, -x + y0, ca, cg, cu, ca);// top
  164. draw_line(-x + x0, -y + y0, x + x0, -y + y0, ca, cg, cu, ca);// upper middle
  165. draw_line(-x + x0, y + y0, x + x0, y + y0, ca, cg, cu, ca);// lower middle
  166. draw_line(-y + x0, x + y0, y + x0, x + y0, ca, cg, cu, ca);// bottom
  167.  
  168. y++;
  169. if (radiusError < 0)
  170. {
  171. radiusError += 2 * y + 1;
  172. } else {
  173. x--;
  174. radiusError+= 2 * (y - x + 1);
  175. }
  176. }
  177. }
  178.  
  179.  
  180. int main()
  181. {
  182. int fbfd = 0;
  183. int x = 0, y = 0, r = 0, cr = 0, cg = 0,cu = 0, w = 0, h = 9;
  184. long int screensize = 0;
  185.  
  186. // Open the file for reading and writing
  187. fbfd = open("/dev/fb0", O_RDWR);
  188. if (fbfd == -1) {
  189. perror("Error: cannot open framebuffer device");
  190. exit(1);
  191. }
  192. // printf("The framebuffer device was opened successfully.\n");
  193.  
  194. // Get fixed screen information
  195. if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo) == -1) {
  196. perror("Error reading fixed information");
  197. exit(2);
  198. }
  199.  
  200. // Get variable screen information
  201. if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo) == -1) {
  202. perror("Error reading variable information");
  203. exit(3);
  204. }
  205.  
  206. // printf("fb %dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel);
  207.  
  208. // Figure out the size of the screen in bytes
  209. screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
  210.  
  211. // Map the device to memory
  212. fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);
  213. if ((long int)fbp == -1) {
  214. perror("Error: failed to map framebuffer device to memory");
  215. exit(4);
  216. }
  217. // printf("The framebuffer device was mapped to memory successfully.\n");
  218.  
  219. // tjp bg color fill ( bg becuz its 1st thing drawn ( under all followers )
  220. fill_rect(0,0,vinfo.xres - 1,vinfo.yres - 7, 100,15,200,0);
  221.  
  222. // horz line mid screen i ask for vinfo.yres but putpixel will limit to vinfo.yres-7
  223. draw_line(0,(vinfo.yres)/2,vinfo.xres,(vinfo.yres)/2,0xff,0xff,0xff,0xff);
  224.  
  225. // vert line midscreen
  226. draw_line(vinfo.xres/2,0,vinfo.xres/2,vinfo.yres,0xff,0xff,0xff,0xff);
  227.  
  228. // big X thru whole screen
  229. draw_line(0,0,vinfo.xres,vinfo.yres,0xff,0xff,0xff,0xff);
  230. draw_line(0,vinfo.yres,vinfo.xres,0,0xff,0xff,0xff,0xff);
  231.  
  232. x = 100;
  233. y = 200;
  234. w = 200;
  235. h = 150;
  236. for ( x = 100; x < vinfo.xres + 100; x = x + 25 ) {
  237. draw_rect(x,y,w,h, 0x00,0xff,0x00,0x08);
  238. y = y + 15;
  239. }
  240.  
  241. x = vinfo.xres - 200 - 100;
  242. y = 100;
  243. w = 200;
  244. h = 50;
  245. for ( x = vinfo.xres - 200 - 100; x > 0; x = x - 25 ) {
  246. fill_rect(x,y,w,h, 0x00,0xff,0x00,0xff);
  247. y = y +17;
  248. }
  249.  
  250. x = 200;
  251. y = vinfo.yres - 200;
  252. for ( r= 10; r<400; r+=15) {
  253. draw_circle(x,y,r,0x00,0x80,0x80,0x80);
  254. x = x + 10;
  255. y = y - 5;
  256. }
  257.  
  258. cr = 100;
  259. cg = 255;
  260. cu = 0x00;
  261. x = vinfo.xres - 200;
  262. y = vinfo.yres - 200;
  263. for ( r = 10; r<250; r+=10){
  264. fill_circle(x,y,r,cu,cu,cu,0xff);
  265. cr = cr + 5;
  266. cg = cg - 8;
  267. cu = cu + 10;
  268. x = x -20;
  269. y = y -20;
  270. }
  271.  
  272. // tjp stupid delay
  273. for( x=0; x<65536; x++)
  274. for( y=0; y<15536; y++);
  275.  
  276. munmap(fbp, screensize);
  277. close(fbfd);
  278. return 0;
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement